<?php
require_once APP  . '/application/admincontrollers/AdminAction.php';
require_once APP  . '/application/models/UserModel.php';
require_once APP  . '/application/models/MeetModel.php';

/**
 * MeetController 会议审核
 * 
 * @author RD / anthony
 * @version 
 */
    
class MeetaController extends AdminAction
{

	/**
     * initial function
     *
     */
    public function init()
    {
    	
    }
    
    /**
     * 显示图片列表
     *
     */
    public  function listAction()
    {
    	$status = (int)$this->getParam("status");
    	
    	$nowp = $this->getParam ('nowp', 1);
        $per_page = 20;

        $image_obj = new MeetModel($this->db);
        $result = $image_obj->fetchAllByStatus($status, $nowp, $per_page);

        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
        $this->view->status = $status;
    }
    
    
    /**
     * 清理会议
     *
     */
    public  function clistAction()
    {    	 
    	$nowp = $this->getParam ('nowp', 1);
    	$mid = (int)$this->getParam("mid");
    	$per_page = 20;
    
    	$image_obj = new MeetModel($this->db);
    	if ($mid > 0){
    		$image_obj->change($mid, array("del" => 1));
    	}
    	
    	
    	$result = $image_obj->fetchAllByExpire($nowp, $per_page);
    
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->nowp = $nowp;
    }
    
    /**
     * 删除
     *
     */
    public function delAction()
    {
    }
    
    public function showAction()
    {
    }
    
    /**
     * 修改状态
     */
    public function statusAction()
    {
    	$this->setAutoRender(false);
    	
    	$mid = $this->getParam("mid");
    	
    	$nstatus = (int)$this->getParam("nstatus");
    
    	$obj_meet = new MeetModel($this->db);
    
    	$obj_meet->change($mid, array("status" => $nstatus, "checktime" => date("Y-m-d H:i:s")));
    	 
    	echo json_encode(array("status" => 1));
    }
    
    
    /**
     * 修改公开状态
     */
    public function publicAction()
    {
    	$this->setAutoRender(false);
    	 
    	$mid = $this->getParam("mid");
    	 
    	$npublic = (int)$this->getParam("npublic");
    
    	$obj_meet = new MeetModel($this->db);
    
    	$obj_meet->change($mid, array("public" => $npublic));
    
    	echo json_encode(array("status" => 1));
    }
    
    public function dchangeAction()
    {
    	$mid = (int)$this->getParam("mid");
    	
    	$obj_meet = new MeetModel($this->db);
    	
    	$url = $this->url($this->view->baseUrl, "meeta", "dchange", "mid={$mid}");
    	
    	if (!empty($_POST['button_submit'])){
	    	$domain = $this->getParam("domain");
	    	
	    	if (empty($domain)){
	    		$this->showmessage($url, "域名不能为空");
	    		return;
	    	}
	    	 
	    	if (!ereg("^([a-zA-Z0-9_-])+$",$domain)){
	    		$this->showmessage($url, "域名只能是英文字母，数字，下划线组成！");
	    		return;
	    	}
	    	 
	    	if (strlen($domain) < 3){
	    		$this->showmessage($url,"域名至少3个字符！");
	    		return;
	    	}
	    	try{
	    		$obj_meet->change($mid, array("domain" => $domain));
	    	}catch(Exception $e){
	    		$this->showmessage($url,"修改失败，域名已存在");
	    		return;
	    	}
	    	
	    	$this->showmessage($url,"修改成功");
    	}
    	$this->view->minfo = $obj_meet->show($mid);
    	$this->view->mid = $mid;
    }
    
    
    public function mailAction()
    {
    	$email = $this->getParam("mail");
    	
    	$this->view->email = $email;
    }
    
    public function domailAction()
    {
    	$this->setAutoRender(false);
    	
    	global $cfg;
    	$toemail = trim($this->getParam("email"));
    	$title = trim($this->getParam("title"));
    	$content = trim($this->getParam("content"));
    	
    	if (empty($toemail)){
    		$url = $this->url($this->view->baseUrl, "meeta", "mail", "mail=" . $toemail, "admin");
    		$this->showmessage($url, "接收邮件地址不能为空！");
    	}
    	 
    	if (!ereg("^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$toemail)){
    		$url = $this->url($this->view->baseUrl, "meeta", "mail", "mail=" . $toemail, "admin");
    		$this->showmessage($url, "邮箱格式错误！");
    	}
    	 
    	if (empty($title) || empty($content)){
    		$url = $this->url($this->view->baseUrl, "meeta", "mail", "mail=" . $toemail, "admin");
    		$this->showmessage($url, "标题或者内容不能为空！");
    	}
    	
    	// 发送邮件
    	require_once APP . '/application/models/mail/class.phpmailer.php';
    	ob_start();
    	$mail = new PHPMailer();
    	
    	if ($cfg['debug'])
    		$mail->SMTPDebug = 2;
    	
    	require_once(APP . '/application/models/PlatsetModel.php');
    	$set = new PlatsetModel($this->db);
    	$set_mail = $set->getmulti(array("plat_email", "plat_emailuser", "plat_emailpwd", 'plat_smtpserver'));
    	
    	$mail->SMTPKeepAlive = true;
    	$mail->IsSMTP();					// 启用SMTP
    	$mail->Host = $set_mail['plat_smtpserver'];			//SMTP服务器
    	$mail->SMTPAuth = true;		//开启SMTP认证
    	$mail->Username = $set_mail['plat_email'];		// SMTP用户名
    	$mail->Password = $set_mail['plat_emailpwd'];	// SMTP密码
    	$mail->From = $set_mail['plat_email'];			//发件人地址
    	$mail->FromName = $set_mail['plat_emailuser'];;				//发件人
    	
    	$mail->WordWrap = 80;					//设置每行字符长度
    	
    	$mail->CharSet = "utf-8";				// 这里指定字符集！
    	$mail->Encoding = "base64";
    	$mail->IsHTML(true);					// 是否HTML格式邮件
    	
    	$mail->AddAddress($toemail);
    	$mail->Subject = $title;
    	$mail->Body = $content;
    	if(!$mail->Send()){
    		$log = ob_get_contents();
    		ob_end_clean();
    			
    		if (!empty($log))
    			file_put_contents(APP . '/cache/testmail.log', $log,FILE_APPEND);
    			
    		$result = array("status" => 0, "message" => '邮件发送失败！请联系管理员');
    		echo json_encode($result);
    		return;
    	}
    	
    	$mail->SmtpClose();
    	$log = ob_get_contents();
    	ob_end_clean();
    	
    	if (!empty($log))
    		file_put_contents(APP . '/cache/testmail.log', $log,FILE_APPEND);
    	
    	$result = array("status" => 1, "message" => '邮件发送成功！');
    	
    	echo json_encode($result);
    }
}
